package CH9;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GUITest {
    public static void main(String[] args) {
        JFrame f=new JFrame("Liubotao");
        f.setBounds(100,90,500,400);
        f.setResizable(false);//设置窗口是否可调整大小
        f.setTitle("请输入注册信息");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体行为：用户单击关闭按钮时，窗体关闭了

        JPanel centerPane = new JPanel();

        GridBagLayout gridBag = new GridBagLayout();    // 布局管理器
        centerPane.setLayout(gridBag);

        JLabel labelUser=new JLabel("用户名：");
        JTextField textUser=new JTextField("aaaaaa");
        JLabel labelPwd=new JLabel("密码：");
        JPasswordField textPwd=new JPasswordField("bbb");
        textPwd.setEchoChar('*');
        JLabel labelSex=new JLabel("性别：");
        JComboBox comboBoxSex=new JComboBox();
        comboBoxSex.addItem("男");//0--->1
        comboBoxSex.addItem("女");//1--->2
        comboBoxSex.insertItemAt("保密",0);//0
        comboBoxSex.setSelectedItem("女");/**/
        comboBoxSex.setSelectedIndex(0);/**/
        JLabel labelRole=new JLabel("身份：");
        JRadioButton rBtnT=new JRadioButton("老师");
        JRadioButton rBtnS=new JRadioButton("学生");
        JLabel labelInterest=new JLabel("兴趣：");
        JCheckBox cBtnSing=new JCheckBox("唱歌");
        JCheckBox cBtnDance=new JCheckBox("跳舞");
        JCheckBox cBtnSwing=new JCheckBox("游泳");
        JButton btnOK=new JButton("确定");
        JButton btnCancel=new JButton("取消");

        centerPane.add(labelUser);
        centerPane.add(textUser);
        centerPane.add(labelPwd);
        centerPane.add(textPwd);
        centerPane.add(labelSex);
        centerPane.add(comboBoxSex);
        centerPane.add(labelRole);
        centerPane.add(rBtnT);
        centerPane.add(rBtnS);
        centerPane.add(labelInterest);
        centerPane.add(cBtnSing);
        centerPane.add(cBtnDance);
        centerPane.add(cBtnSwing);
        centerPane.add(btnOK);
        centerPane.add(btnCancel);

        GridBagConstraints gbc= new GridBagConstraints(); // 布局约束
        gbc.insets=new Insets(10,5,10,5);
        gbc.fill = GridBagConstraints.HORIZONTAL;//让组件在横向上填充整个网格，默认是去组件默认大小
        //gbc.gridy=3;//行3
        //gbc.gridx=2;//列2
        //gbc.gridwidth=1;//占1列
        //gridBag.addLayoutComponent(labelUser, gbc); // 内部使用的仅是 c 的副本
        addLayoutComponent(gridBag,gbc,labelUser, 3,2,1);

        //gbc.gridy=3;//行3
        //gbc.gridx=3;//列3
        //gbc.gridwidth=3;//占3列
        //gridBag.addLayoutComponent(textUser, gbc); // 内部使用的仅是 c 的副本
        addLayoutComponent(gridBag,gbc,textUser, 3,3,3);

        //gbc.gridy=4;//行4
        //gbc.gridx=2;//列2
        //gbc.gridwidth=1;//占1列
        //gridBag.addLayoutComponent(labelPwd, gbc); // 内部使用的仅是 c 的副本
        addLayoutComponent(gridBag,gbc,labelPwd, 4,2,1);

        //行4列3,3个
        addLayoutComponent(gridBag,gbc,textPwd, 4,3,3);
        //行5列2	1个：行5列2
        addLayoutComponent(gridBag,gbc,labelSex, 5,2,1);
        //行5列3	3个：行5列3开始的连续3列
        addLayoutComponent(gridBag,gbc,comboBoxSex, 5,3,3);
        //行6列2	1个：行6列2
        addLayoutComponent(gridBag,gbc,labelRole, 6,2,1);
        //行6列3	1个：行6列3
        addLayoutComponent(gridBag,gbc,rBtnT, 6,3,1);
        //行6列4	1个：行6列4
        addLayoutComponent(gridBag,gbc,rBtnS, 6,4,1);
        //行7列2	1个：行7列2
        addLayoutComponent(gridBag,gbc,labelInterest, 7,2,1);
        //行7列3	1个：行7列3
        addLayoutComponent(gridBag,gbc,cBtnSing, 7,3,1);
        //行7列4	1个：行7列4
        addLayoutComponent(gridBag,gbc,cBtnDance, 7,4,1);
        //行7列5	1个：行7列5
        addLayoutComponent(gridBag,gbc,cBtnSwing, 7,5,1);
        //行8列3	1个：行8列3
        addLayoutComponent(gridBag,gbc,btnOK, 8,3,1);
        //行8列4	1个：行8列4
        addLayoutComponent(gridBag,gbc,btnCancel, 8,4,1);

        f.setContentPane(centerPane);
         //  f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//设置窗体行为：用户单击关闭按钮时，窗体啥都不干
        f.setVisible(true);//设置窗口为可见，默认不可见/**/

        ActionListenerOK listenerOK=new ActionListenerOK();
        btnOK.addActionListener(listenerOK);

        ActionListenerCanle listenerCanle=new ActionListenerCanle();
        btnCancel.addActionListener(listenerCanle);

        System.out.println();
    }


    public static void addLayoutComponent(GridBagLayout gridBag,
                                          GridBagConstraints gbc,
                                          Component component,
                                          int row,int col,
                                          int colNum){
        gbc.gridy=row;//行4
        gbc.gridx=col;//列2
        gbc.gridwidth=colNum;//占1列
        gridBag.addLayoutComponent(component, gbc); // 内部使用的仅是 c 的副本
    }
}

class  ActionListenerOK implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(
                null,
                "String message",
                "String title",
                //JOptionPane.WARNING_MESSAGE);
                JOptionPane.ERROR_MESSAGE);
    }
}

class  ActionListenerCanle implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(
                null,
                "String message",
                "String title",
                //JOptionPane.WARNING_MESSAGE);
                JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }
}